﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPWeb.Class;
using KPWeb.Data;
using KPWeb.Function;
using MSExcel = Microsoft.Office.Interop.Excel;



namespace KPWeb.ListyObecnosci
{

    public partial class PracownicyPlanyExcel : KPPage
    {
        private readonly string[] MonthNames = { "Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień" };
        private short RokPlany
        {
            get
            {
                if (ViewState["RokPlany"] == null)
                    ViewState["RokPlany"] = DateTime.Now.Year;
                return short.Parse(ViewState["RokPlany"].ToString());
            }
            set
            {
                ViewState["RokPlany"] = value;

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Error1.Visible = false;
            fError.Visible = false;
            if (!IsPostBack)
            {

                dGrupa.Items.Clear();
                dGrupa.Items.Add(new ListItem() { Text = Stale.wybierzGrupe, Value = "0" });
                foreach (vKP_JednostkiGrupy item in Model.GetGrupyDlaFirmy(int.Parse(Current.FirmaID)))
                {
                    dGrupa.Items.Add(new ListItem()
                    {
                        Text = item.GrupaOpis + " (" + item.GrupaID.ToString() + ")",
                        Value = item.GrupaID.ToString()
                    });
                }


                //RokPlany = short.Parse(Request["value"]);
                //RokPlany += short.Parse(DateTime.Now.Year.ToString());


                dYear.Items.Clear();
                foreach (short item in Kalendarz.GetYearGdzieWpisanoSwieta())
                {
                    this.dYear.Items.Add(item.ToString());
                }

                dYear.SelectedValue = RokPlany.ToString();

                //string[] MonthNames
                dMonth1.Items.Clear();
                dMonth2.Items.Clear();
                for (int i = 0; i < 12; i++)
                {
                    dMonth1.Items.Add(new ListItem()
                     {
                         Text = MonthNames[i],
                         Value = (i + 1).ToString()
                     });

                    dMonth2.Items.Add(new ListItem()
                    {
                        Text = MonthNames[i],
                        Value = (i + 1).ToString()
                    });
                }

                dYear.SelectedValue = RokPlany.ToString();

            }

        }
        protected int PracownicyPlanyNieobecnosciWyswietlExcel(byte typ, string grupa, int year, int month1, int month2) //0 bez tytułów, 1 ztytułami
        // 0 OK
        {
            int iTemp = 0;
      

            MSExcel.Application excelApp = new MSExcel.Application();
            string filePath = Server.MapPath("~/EXCEL/SzUrlopyPlany.xls");

            MSExcel.Workbook excelWorkbook = excelApp.Workbooks.Open(filePath,
                    0, true, 5, "", "", false, MSExcel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            MSExcel.Sheets excelSheets = excelWorkbook.Worksheets;

            string currentSheet = "ZestawienieUrlopy";
            MSExcel.Worksheet excelWorksheet = (MSExcel.Worksheet)excelSheets.get_Item(currentSheet);

            MSExcel.Range excelCell ;
            MSExcel.Range excelCellTarget;
            MSExcel.Range excelCellSource;
            //f,r ad,2,3
            excelCell = (MSExcel.Range)excelWorksheet.get_Range("F2", "F2");
            excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
            excelCell.Offset[1, 1].Value="";
       
            excelCell = (MSExcel.Range)excelWorksheet.get_Range("R2", "R2");
            excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.SandyBrown);
            excelCell.Offset[0, 1].Value = "Konflikty";
            excelCell.Offset[1, 1].Value = "";
          
            excelCell = (MSExcel.Range)excelWorksheet.get_Range("AD1", "AD1");
            excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MediumSpringGreen);
            excelCell.Offset[1, 0].Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Offset[1, 0].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LimeGreen);
            excelCell.Offset[2, 0].Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Offset[2, 0].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.GreenYellow);
         
            
            excelCell = (MSExcel.Range)excelWorksheet.get_Range("C1", "C1");
            vKP_JednostkiFirmy item1 = Model.GetJednostkaFirma(int.Parse(KPPage.Current.JednostkaID));
            excelCell.Value = item1.JednostkaNazwa;
            vKP_Swieta[] ListaSwiat; 

            for (int iMonth = month1; iMonth < month2; iMonth++)
            {
                ListaSwiat = Kalendarz.GetSwieta(year, iMonth);

                int lastDayMonth = FunctionGlobal.IleDniMiesiac(year, iMonth);


                excelCell = (MSExcel.Range)excelWorksheet.get_Range("C4", "C4").Offset[iTemp, 0];
                excelCell.Value = MonthNames[iMonth - 1] + " / " + year.ToString();
                excelCell.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Navy);

                DateTime d = new DateTime();
                for (int i = 0; i < lastDayMonth; i++)
                {
                    excelCell = (MSExcel.Range)excelWorksheet.get_Range("G5", "G5").Offset[iTemp, i];
                    excelCell.Value = (i + 1).ToString();

                }
                for (int i = 0; i < lastDayMonth; i++)
                {
                    d = new DateTime(year, iMonth, i + 1);
                    excelCell = (MSExcel.Range)excelWorksheet.get_Range("G6", "G6").Offset[iTemp, i];
                    excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
                  
                    if (d.DayOfWeek == DayOfWeek.Saturday)
                    {
                        excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MediumSpringGreen);
                    }
                    if (d.DayOfWeek == DayOfWeek.Sunday)
                    {
                        excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LimeGreen);
                    }
                }

                foreach (vKP_Swieta sw in ListaSwiat)
                {
                    excelCell = (MSExcel.Range)excelWorksheet.get_Range("G6", "G6").Offset[iTemp, sw.day - 1];
                    excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.GreenYellow);
                }


                Model model = new Model();


                excelCellSource = (MSExcel.Range)excelWorksheet.get_Range("G6", "AK6").Offset[iTemp, 0];
                excelCell = (MSExcel.Range)excelWorksheet.get_Range("C6", "C6");
                int iInic = 0;
                foreach (vKP_OsobyListaObecnosci item in model.GetListaObecnosci(int.Parse(Current.JednostkaID), int.Parse(grupa)))
                {
                    if (iInic > 0)
                    {
                        excelCellTarget = (MSExcel.Range)excelWorksheet.get_Range("G6", "AK6").Offset[iTemp, 0];
                        excelCellSource.Copy(excelCellTarget);
                    }
                    foreach (KP_PlanyUrlopow iUr in Model.GetPlanowaneUrlopy(item.OsobaID, year, iMonth))
                    {
                        excelCell.Offset[iTemp, iUr.Date.Day + 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                    }
                    excelCell.Offset[iTemp, 0].Value =
                              (typ == 1 ? item.Tytul.ToLower() : "") + "\r\n" + FunctionGlobal.UppercaseFirstWords(item.NazwiskoImie);
                    excelCell.Offset[iTemp, 3].Value = (iInic + 1).ToString();
                    iInic += 1;
                    iTemp += 1;

                }
      
                iTemp += 4;
            }

            excelApp.Visible = true;
            return 0;
        }




        protected void ListaObecnosciView_Click(object sender, System.EventArgs e)
        {
            if (dGrupa.SelectedItem.ToString() == Stale.wybierzGrupe)
            {
                Error1.Visible = true;
                fError.Visible = true;
                return;
            }
            int iTemp = PracownicyPlanyNieobecnosciWyswietlExcel(0, dGrupa.SelectedValue, int.Parse(dYear.SelectedValue), int.Parse(dMonth1.SelectedValue),
                   int.Parse(dMonth2.SelectedValue) + 1);
        }
        protected void ListaObecnosciViewTytul_Click(object sender, System.EventArgs e)
        {
            if (dGrupa.SelectedItem.ToString() == Stale.wybierzGrupe)
            {
                Error1.Visible = true;
                fError.Visible = true;
                return;
            }
            int iTemp = PracownicyPlanyNieobecnosciWyswietlExcel(1, dGrupa.SelectedValue, int.Parse(dYear.SelectedValue), int.Parse(dMonth1.SelectedValue),
                   int.Parse(dMonth2.SelectedValue)+1);
        }

        protected void ListaObecnosciBezNazwisk_Click(object sender, System.EventArgs e)
        {

            int iTemp = PracownicyPlanyNieobecnosciWyswietlExcel(2, dGrupa.SelectedValue, int.Parse(dYear.SelectedValue), int.Parse(dMonth1.SelectedValue),
                   int.Parse(dMonth2.SelectedValue)+1);
        }
    }
}